#include "console.h"
#include <debug.h>

Console::Console() {
	xpos = 0;
	ypos = 0;
	video = (unsigned char*) VIDEO;
	clearConsole();
}

Console::~Console() {
	delete video;
}

void Console::clearConsole() {
	// clear screen
	for (int i = 0; i < COLUMNS * LINES * 2; i++) {
		*(video + i) = 0; 
	}
	// reset cursor position
	xpos = 0;
	ypos = 0;
}

void Console::newline() {
	xpos = 0;
	ypos++;
	if (ypos >= LINES) {
		ypos = 0;
		Debug::dprint("cs.newline(): scroll");
	}
}

/* Put the given character, c, on the screen. */
void Console::putchar (int c) {
	if (c == '\n') {
		newline();
	} else {
		*(video + (xpos + ypos * COLUMNS) * 2) = c & 0xFF;
		*(video + (xpos + ypos * COLUMNS) * 2 + 1) = ATTRIBUTE;
		xpos++;
	} 
	// if reaches the end of line
	if (xpos >= COLUMNS) {
		newline();
	}
}


